
/*结构体
// 用户自定义的数据类型，允许用于存储不同的数据类型
// 三种定义方式：
struct 类型名称{成员列表}
函数参数：
值传递 函数中改变值，不会影响其他地方值
地址传递 函数中改变值，会同步改变其他地方值

结构体数组
*/
#include <iostream>
#include <stdio.h>
#include <string>
// #include <math.h>
using namespace std;
struct Student {
  string name;
  int age;
  int score;
} s3; //
struct Teacher {
  int id;
  string name;
  int age;
  struct Student stu;
}; //

void printStudent1(struct Student s) {
  s.age = 100;
  cout << "传入参数1：" << s.name << " " << s.age << " " << s.score << endl;
}
// 将函数中的形参改为指针，可以减少内存空间，不会复制新的副本出来
// 加const防止函数体中的误操作
// void printStudent2(const struct Student *s)
void printStudent2(struct Student *s) {
  s->age = 200;
  cout << "传入参数2：" << s->name << " " << s->age << " " << s->score << endl;
}

int main() {
  // 1
  struct Student s1; // 此处struct可以省略
  s1.name = "zhang san";
  s1.age = 18;
  s1.age = 18;
  s1.score = 100;
  cout << s1.name << " " << s1.age << " " << s1.score << endl;
  // 2
  const struct Student s2 = {"li si", 19, 80};
  cout << s2.name << " " << s2.age << " " << s2.score << endl;
  // 3
  s3.name = "wang wu";
  s3.age = 18;
  s3.score = 100;
  cout << s3.name << " " << s3.age << " " << s3.score << endl;

  // 结构体数组
  struct Student stuArray[3] = {
      {"zhangsan", 15, 80}, {"li si", 35, 90}, {"li si", 45, 99}};
  // 修改结构体内容
  stuArray[2].name = "zhaoliu";
  // 遍历结构体数组
  for (int i = 0; i < 3; i++) {
    cout << "姓名： " << stuArray[i].name << "年龄： " << stuArray[i].age
         << "分数：" << stuArray[i].score << endl;
  }
  // 结构体指针
  Student *p = &s1;
  cout << "结构体指针： " << p->name << " " << p->age << " " << p->score
       << endl;

  // 结构体嵌套
  Teacher t;
  t.id = 100;
  t.name = "王老师";
  t.age = 50;
  t.stu.name = "小明";
    t.stu.age = 20;
    t.stu.score = 60;
    cout << "老师姓名： " << t.name << ", 老师年龄 " << t.age << ", 老师编号 " << t.id << endl;
    cout << "学生姓名： " << t.stu.name << ", 学生年龄" << t.stu.age << ", 学生分数" << t.stu.score << endl;

    // 函数 值传递
    printStudent1(s1);
    // 地址传递
    printStudent2(&s1);
    cout << "根据地址传递改变年龄之后： " << s1.name << " " << s1.age << " " << s1.score << endl;
    ;
}